package vn.egame.elockscreen.service;

import vn.egame.elockscreen.receive.LockerReceive;
import vn.egame.elockscreen.receive.PhoneStateReceive;
import android.app.IntentService;
import android.app.Notification;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;

/**
 * An {@link IntentService} subclass for handling asynchronous task requests in
 * a service on a separate handler thread.
 * <p/>
 * TODO: Customize class - update intent actions, extra parameters and static
 * helper methods.
 */
public class ScreenLockerService extends Service {

	// IntentService can perform, e.g. ACTION_FETCH_NEW_ITEMS
	private static final String ACTION_START = "com.simon.wu.screenlocker.screenlocker.activity.action.START";
	private static final String ACTION_STOP = "com.simon.wu.screenlocker.screenlocker.activity.action.STOP";
	private IntentFilter filterScreen, filterPhoneState;
	public static boolean isRunning;
//	public static boolean isShowLock;
	public static boolean isCall;
	public static int timeWait;
	

	private BroadcastReceiver lockerReceive = new LockerReceive();
	private BroadcastReceiver phoneStateReceive = new PhoneStateReceive();

	// private BroadcastReceiver screenReceiver = new BroadcastReceiver() {
	// @Override
	// public void onReceive(Context context, Intent intent) {
	//
	// }
	// };

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub

		isRunning = true;
		IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
		filter.addAction(Intent.ACTION_SCREEN_OFF);
		// filter.addAction(Intent.ACTION_BOOT_COMPLETED);

		registerReceiver(lockerReceive, filter);

		filterPhoneState = new IntentFilter("android.intent.action.PHONE_STATE");
		registerReceiver(phoneStateReceive, filterPhoneState);

		super.onCreate();
	}

	@Override
	public void onStart(Intent intent, int startId) {
		// TODO Auto-generated method stub
		super.onStart(intent, startId);

	}

	/**
	 * Starts this service to perform action Foo with the given parameters. If
	 * the service is already performing a task this action will be queued.
	 * 
	 * @see IntentService
	 */
	public static void startActionStart(Context context, String param1,
			String param2) {

		if (!isRunning) {

			Log.d("Kai", "Start service");
			Intent intent = new Intent(context, ScreenLockerService.class);
			intent.setAction(ACTION_START);
			context.startService(intent);
		}
	}

	/**
	 * Starts this service to perform action Baz with the given parameters. If
	 * the service is already performing a task this action will be queued.
	 * 
	 * @see IntentService
	 */

	public static void startActionStop(Context context, String param1,
			String param2) {
		if (isRunning) {
			Intent intent = new Intent(context, ScreenLockerService.class);
			intent.setAction(ACTION_STOP);
			context.startService(intent);
		}
	}

	public ScreenLockerService() {
		filterScreen = new IntentFilter(Intent.ACTION_SCREEN_ON);
		filterScreen.addAction(Intent.ACTION_SCREEN_OFF);
		// filter.addAction(Intent.ACTION_BOOT_COMPLETED);

		filterPhoneState = new IntentFilter("android.intent.action.PHONE_STATE");

	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {

//		Notification note = new Notification(0, null,
//				System.currentTimeMillis());
//		note.flags |= Notification.FLAG_NO_CLEAR ;
//		startForeground(1242, note);

		handleActionFoo();

		// PendingIntent localPendingIntent = PendingIntent.getService(this, 0,
		// new Intent(this, UsbService.class), 0);
		// AlarmManager localAlarmManager = (AlarmManager)
		// getSystemService(Context.ALARM_SERVICE);
		// Calendar localCalendar = Calendar.getInstance();
		// localCalendar.setTimeInMillis(System.currentTimeMillis());
		// localCalendar.add(13, 50);
		// localAlarmManager.set(0, localCalendar.getTimeInMillis(),
		// localPendingIntent);

		// if (intent != null) {
		// String action = intent.getAction();
		// if (ACTION_START.equals(action)) {
		// handleActionFoo();
		// } else if (ACTION_STOP.equals(action)) {
		// handleActionBaz();
		// }
		// }else{
		// startActionStart(getApplicationContext(), null, null);
		// }
		// return super.onStartCommand(intent, flags, startId);

		return START_STICKY;
	}

	/**
	 * Handle action Foo in the provided background thread with the provided
	 * parameters.
	 */
	private void handleActionFoo() {
		isRunning = true;
		registerReceiver(lockerReceive, filterScreen);
		registerReceiver(phoneStateReceive, filterPhoneState);
	}

	/**
	 * Handle action Baz in the provided background thread with the provided
	 * parameters.
	 */
	private void handleActionBaz() {
		isRunning = false;
		unregisterReceiver(lockerReceive);
	}

	@Override
	public void onDestroy() {
		Log.d("Kai", "Det");

		super.onDestroy();

		// if (isRunning) {
		// unregisterReceiver(LockerReceive);
		// // unregisterReceiver(screenReceiver);
		// }
		// isRunning = false;
	}

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
}
